package mww.servlet.course;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.entity.Course;
import mww.javabean.service.CourseService;
import mww.javabean.service.CourseServiceImpl;
public class AddCourseServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CourseService service = new CourseServiceImpl();
		HttpSession session=request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String courseNo=request.getParameter("courseNo");
		String courseName=request.getParameter("courseName");
		Integer studyTime=new Integer(request.getParameter("studyTime"));
		Float credit=new Float(request.getParameter("credit"));
		Integer courseType=new Integer(request.getParameter("courseType"));
		Integer term=new Integer(request.getParameter("term"));
		try{
			if(service.findCourseByCourseNo(courseNo)!=null){	//检测课程是否已存在
				session.setAttribute("message", "所要添加的课程已存在！");
				request.getRequestDispatcher("/admin/addCourse.jsp").forward(request, response);
			}else{
				Course course=new Course(courseNo,courseName,studyTime,credit,courseType,term);
				service.addCourse(course);
		  		session.setAttribute("message", "课程信息添加成功！");
		  		request.getRequestDispatcher("/admin/listAllCourseServlet").forward(request, response);
			}
		}catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}

}
